import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/pages/out_tab_page.dart';
import 'package:material_collection_app/widget/common/search_box.dart';
import 'package:material_collection_app/widget/common/tabbar_custom.dart';
import 'package:material_collection_app/widget/common/top_bar.dart';
import 'package:material_collection_app/widget/wareout/wareout_dialog.dart';

class OutPage extends StatefulWidget {
  final Map arguments;
  OutPage({Key key, this.arguments}) : super(key: key);

  @override
  State<StatefulWidget> createState() => MyState();
}

class MyState extends State<OutPage> with SingleTickerProviderStateMixin {
  String validStartDate;
  String validEndDate;
  String enStartDate;
  String enEndDate;
  String actionModel = 'destroy';
  String keywords = '';
  List tabsStatus = [
    {'status': '33', 'index': 0},
    {'status': '3', 'index': 1},
    {'status': '4', 'index': 2}
  ];
  List tabs = ['待接收', '待出库', '已出库'];
  String baseApi = 'goodsOut/';

  TabController _tabController;
  TextEditingController _searchController = TextEditingController();

  @override
  void initState() {
    super.initState();
    String currentTime = new DateTime.now().toString().substring(0, 10);

    validStartDate = currentTime;
    validEndDate = currentTime;
    enStartDate = currentTime;
    enEndDate = currentTime;

    _tabController = new TabController(vsync: this, length: tabs.length);

    if (widget.arguments != null) {
      String model = widget.arguments['model'];

      if (model != '') {
        setState(() {
          actionModel = model;
        });
      }
    } else {
      Timer.periodic(Duration(milliseconds: 100), (timer) {
        this.showWarehouseOutDialog();
        timer.cancel();
      });
    }
  }

  Future<void> showWarehouseOutDialog() async {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return WarehouseOutDialog(
              defaultType: actionModel,
              selectType: (type) => {selectEnterType(type)});
        });
  }

  // 选择入库类型
  selectEnterType(String type) {
    int index = _tabController.index;

    if (type == 'scrap') {
      baseApi = 'ScrapGoodsOut/';
    } else if (type == 'allocate') {
      baseApi = 'AllocateGoodsOut/';
    } else {
      baseApi = 'goodsOut/';
    }

    setState(() {
      actionModel = type;
    });

    if (index == 0) stautsKey1.currentState.search();
    if (index == 1) stautsKey2.currentState.search();
    if (index == 2) stautsKey3.currentState.search();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);
    List<Key> keys = [stautsKey1, stautsKey2, stautsKey3];

    return Scaffold(
        appBar: TopBar(
          height: 0,
        ),
        body: SafeArea(
            child: Container(
                color: Color(0xFF297CFF),
                child: Column(children: <Widget>[
                  SearchBox(
                      hintText: '名称',
                      endDrawer: true,
                      searchController: _searchController,
                      onSearch: (value) {
                        setState(() {
                          keywords = value;
                        });

                        int index = _tabController.index;
                        if (index == 0) stautsKey1.currentState.search();
                        if (index == 1) stautsKey2.currentState.search();
                        if (index == 2) stautsKey3.currentState.search();
                      }),
                  Expanded(
                      child: DefaultTabController(
                          length: 3,
                          initialIndex: 0,
                          child: Column(children: <Widget>[
                            TabBarCustom(
                              tabs: tabs,
                              tabController: _tabController,
                            ),
                            Expanded(
                                child: TabBarView(
                                    controller: _tabController,
                                    children: tabsStatus.map((item) {
                                      return OutTabPage(
                                        baseApi: baseApi,
                                        actionModel: actionModel,
                                        status: item['status'],
                                        keywords: keywords,
                                        key: keys[item['index']],
                                      );
                                    }).toList()))
                          ])))
                ]))));
  }
}
